package com.flux.ui.view

import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import android.widget.LinearLayout
import android.widget.TextView

/**
 * 字母排序控件
 * Created on 2017/3/13.
 *
 * @author yang
 */

class LetterView(private val mContext: Context, attrs: AttributeSet) : LinearLayout(mContext, attrs) {
    private var mListener: CharacterClickListener? = null

    init {
        orientation = LinearLayout.VERTICAL
        initView()
    }

    private fun initView() {

        var i = 'A'
        while (i <= 'Z') {
            val character = i + ""
            val tv = buildTextLayout(character)

            addView(tv)
            i++
        }

        addView(buildTextLayout("#"))
    }

    private fun buildTextLayout(character: String): TextView {
        val layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1f)

        val tv = TextView(mContext)
        tv.layoutParams = layoutParams
        tv.gravity = Gravity.CENTER
        tv.isClickable = true

        tv.text = character

        tv.setOnClickListener {
            if (mListener != null) {
                mListener!!.clickCharacter(character)
            }
        }
        return tv
    }

    fun setCharacterListener(listener: CharacterClickListener) {
        mListener = listener
    }

    interface CharacterClickListener {
        fun clickCharacter(character: String)
    }
}